
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>12. 虚拟环境和包 &#8212; Python 3.7.3 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.3 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="13. 接下来？" href="whatnow.html" />
    <link rel="prev" title="11. 标准库简介 —— 第二部分" href="stdlib2.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/tutorial/venv.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="whatnow.html" title="13. 接下来？"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="stdlib2.html" title="11. 标准库简介 —— 第二部分"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 教程</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="virtual-environments-and-packages">
<span id="tut-venv"></span><h1>12. 虚拟环境和包<a class="headerlink" href="#virtual-environments-and-packages" title="永久链接至标题">¶</a></h1>
<div class="section" id="introduction">
<h2>12.1. 概述<a class="headerlink" href="#introduction" title="永久链接至标题">¶</a></h2>
<p>Python应用程序通常会使用不在标准库内的软件包和模块。应用程序有时需要特定版本的库，因为应用程序可能需要修复特定的错误，或者可以使用库的过时版本的接口编写应用程序。</p>
<p>这意味着一个Python安装可能无法满足每个应用程序的要求。如果应用程序A需要特定模块的1.0版本但应用程序B需要2.0版本，则需求存在冲突，安装版本1.0或2.0将导致某一个应用程序无法运行。</p>
<p>这个问题的解决方案是创建一个 <a class="reference internal" href="../glossary.html#term-virtual-environment"><span class="xref std std-term">virtual environment</span></a>，一个目录树，其中安装有特定Python版本，以及许多其他包。</p>
<p>然后，不同的应用可以使用不同的虚拟环要解决先前的冲突需求示例，应用程序A可以拥有自己的1.0版本安装虚拟环境，而应用程序B则具有2.0版本的另一个虚拟环境。如果应用程序B要求将库升级到3.0版本，也不会影响应用程序A的环境。</p>
</div>
<div class="section" id="creating-virtual-environments">
<h2>12.2. 创建虚拟环境<a class="headerlink" href="#creating-virtual-environments" title="永久链接至标题">¶</a></h2>
<p>用于创建和管理虚拟环境的模块称为 <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a>。<a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> 通常会安装你可用的最新版本的 Python。如果您的系统上有多个版本的 Python，您可以通过运行 <code class="docutils literal notranslate"><span class="pre">python3</span></code> 或您想要的任何版本来选择特定的Python版本。</p>
<p>要创建虚拟环境，请确定要放置它的目录，并将 <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> 模块作为脚本运行目录路径:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python3</span> <span class="o">-</span><span class="n">m</span> <span class="n">venv</span> <span class="n">tutorial</span><span class="o">-</span><span class="n">env</span>
</pre></div>
</div>
<p>如果它不存在，这将创建 <code class="docutils literal notranslate"><span class="pre">tutorial-env</span></code> 目录，并在其中创建包含Python解释器，标准库和各种支持文件的副本的目录。</p>
<p>创建虚拟环境后，您可以激活它。</p>
<p>在Windows上，运行:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">tutorial</span><span class="o">-</span><span class="n">env</span>\<span class="n">Scripts</span>\<span class="n">activate</span><span class="o">.</span><span class="n">bat</span>
</pre></div>
</div>
<p>在Unix或MacOS上，运行:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">source</span> <span class="n">tutorial</span><span class="o">-</span><span class="n">env</span><span class="o">/</span><span class="nb">bin</span><span class="o">/</span><span class="n">activate</span>
</pre></div>
</div>
<p>（这个脚本是为bash shell编写的。如果你使用 <strong class="program">csh</strong> 或 <strong class="program">fish</strong> shell，你应该改用 <code class="docutils literal notranslate"><span class="pre">activate.csh</span></code> 或 <code class="docutils literal notranslate"><span class="pre">activate.fish</span></code> 脚本。）</p>
<p>Activating the virtual environment will change your shell's prompt to show what
virtual environment you're using, and modify the environment so that running
<code class="docutils literal notranslate"><span class="pre">python</span></code> will get you that particular version and installation of Python.
For example:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ <span class="nb">source</span> ~/envs/tutorial-env/bin/activate
<span class="o">(</span>tutorial-env<span class="o">)</span> $ python
Python <span class="m">3</span>.5.1 <span class="o">(</span>default, May  <span class="m">6</span> <span class="m">2016</span>, <span class="m">10</span>:59:36<span class="o">)</span>
  ...
&gt;&gt;&gt; import sys
&gt;&gt;&gt; sys.path
<span class="o">[</span><span class="s1">&#39;&#39;</span>, <span class="s1">&#39;/usr/local/lib/python35.zip&#39;</span>, ...,
<span class="s1">&#39;~/envs/tutorial-env/lib/python3.5/site-packages&#39;</span><span class="o">]</span>
&gt;&gt;&gt;
</pre></div>
</div>
</div>
<div class="section" id="managing-packages-with-pip">
<h2>12.3. 使用pip管理包<a class="headerlink" href="#managing-packages-with-pip" title="永久链接至标题">¶</a></h2>
<p>你可以使用一个名为 <strong class="program">pip</strong> 的程序来安装、升级和移除软件包。默认情况下 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 将从 Python Package Index &lt;<a class="reference external" href="https://pypi.org">https://pypi.org</a>&gt; 安装软件包。你可以在浏览器中访问 Python Package Index 或是使用 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 受限的搜索功能:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip search astronomy
skyfield               - Elegant astronomy <span class="k">for</span> Python
gary                   - Galactic astronomy and gravitational dynamics.
novas                  - The United States Naval Observatory NOVAS astronomy library
astroobs               - Provides astronomy ephemeris to plan telescope observations
PyAstronomy            - A collection of astronomy related tools <span class="k">for</span> Python.
...
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span></code> 有许多子命令：“search”、“install”、“uninstall”、“freeze”等等。（请参阅 <a class="reference internal" href="../installing/index.html#installing-index"><span class="std std-ref">安装 Python 模块</span></a> 指南以了解  <code class="docutils literal notranslate"><span class="pre">pip</span></code> 的完整文档。）</p>
<p>您可以通过指定包的名称来安装最新版本的包：</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip install novas
Collecting novas
  Downloading novas-3.1.1.3.tar.gz <span class="o">(</span>136kB<span class="o">)</span>
Installing collected packages: novas
  Running setup.py install <span class="k">for</span> novas
Successfully installed novas-3.1.1.3
</pre></div>
</div>
<p>您还可以通过提供包名称后跟 <code class="docutils literal notranslate"><span class="pre">==</span></code> 和版本号来安装特定版本的包：</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip install <span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.6.0
Collecting <span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.6.0
  Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0
</pre></div>
</div>
<p>如果你重新运行这个命令，<code class="docutils literal notranslate"><span class="pre">pip</span></code> 会注意到已经安装了所请求的版本并且什么都不做。您可以提供不同的版本号来获取该版本，或者您可以运行 <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">--upgrade</span></code> 将软件包升级到最新版本：</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip install --upgrade requests
Collecting requests
Installing collected packages: requests
  Found existing installation: requests <span class="m">2</span>.6.0
    Uninstalling requests-2.6.0:
      Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">uninstall</span></code> 后跟一个或多个包名称将从虚拟环境中删除包。</p>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">show</span></code> 将显示有关特定包的信息：</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip show requests
---
Metadata-Version: <span class="m">2</span>.0
Name: requests
Version: <span class="m">2</span>.7.0
Summary: Python HTTP <span class="k">for</span> Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: Apache <span class="m">2</span>.0
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
Requires:
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">list</span></code> 将显示虚拟环境中安装的所有软件包：</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip list
novas <span class="o">(</span><span class="m">3</span>.1.1.3<span class="o">)</span>
numpy <span class="o">(</span><span class="m">1</span>.9.2<span class="o">)</span>
pip <span class="o">(</span><span class="m">7</span>.0.3<span class="o">)</span>
requests <span class="o">(</span><span class="m">2</span>.7.0<span class="o">)</span>
setuptools <span class="o">(</span><span class="m">16</span>.0<span class="o">)</span>
</pre></div>
</div>
<p><cite>pip freeze`</cite> 将生成一个类似的已安装包列表，但输出使用 <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span></code> 期望的格式。一个常见的约定是将此列表放在 <code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code> 文件中：</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip freeze &gt; requirements.txt
<span class="o">(</span>tutorial-env<span class="o">)</span> $ cat requirements.txt
<span class="nv">novas</span><span class="o">==</span><span class="m">3</span>.1.1.3
<span class="nv">numpy</span><span class="o">==</span><span class="m">1</span>.9.2
<span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.7.0
</pre></div>
</div>
<p>然后可以将 <code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code> 提交给版本控制并作为应用程序的一部分提供。然后用户可以使用 <code class="docutils literal notranslate"><span class="pre">install</span> <span class="pre">-r</span></code> 安装所有必需的包：</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span> $ pip install -r requirements.txt
Collecting <span class="nv">novas</span><span class="o">==</span><span class="m">3</span>.1.1.3 <span class="o">(</span>from -r requirements.txt <span class="o">(</span>line <span class="m">1</span><span class="o">))</span>
  ...
Collecting <span class="nv">numpy</span><span class="o">==</span><span class="m">1</span>.9.2 <span class="o">(</span>from -r requirements.txt <span class="o">(</span>line <span class="m">2</span><span class="o">))</span>
  ...
Collecting <span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.7.0 <span class="o">(</span>from -r requirements.txt <span class="o">(</span>line <span class="m">3</span><span class="o">))</span>
  ...
Installing collected packages: novas, numpy, requests
  Running setup.py install <span class="k">for</span> novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span></code> 有更多选择。有关 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 的完整文档，请参阅 <a class="reference internal" href="../installing/index.html#installing-index"><span class="std std-ref">安装 Python 模块</span></a> 指南。当您编写一个包并希望在 Python 包索引中使它可用时，请参考 <a class="reference internal" href="../distributing/index.html#distributing-index"><span class="std std-ref">分发 Python 模块</span></a> 指南。</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">12. 虚拟环境和包</a><ul>
<li><a class="reference internal" href="#introduction">12.1. 概述</a></li>
<li><a class="reference internal" href="#creating-virtual-environments">12.2. 创建虚拟环境</a></li>
<li><a class="reference internal" href="#managing-packages-with-pip">12.3. 使用pip管理包</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="stdlib2.html"
                        title="上一章">11. 标准库简介 —— 第二部分</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="whatnow.html"
                        title="下一章">13. 接下来？</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/tutorial/venv.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="whatnow.html" title="13. 接下来？"
             >下一页</a> |</li>
        <li class="right" >
          <a href="stdlib2.html" title="11. 标准库简介 —— 第二部分"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 教程</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2019, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 4月 09, 2019.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>1.8.4 创建。
    </div>

  </body>
</html>